#include "EditorTextButton.h"

#include "Widgets/RectangleWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/Mouse/MouseWidget.h"


TSharedObjectPtr<RWidget> WHE::CreateTextButton(
    const CString &Text
    , std::function<void()> InOnClick
    , TSharedObjectPtr<RWidget> InParentWidget
    )
{
    auto Button = NewWidget<RRectangleWidget>(InParentWidget);
    Button->Width = 100;
    Button->Height = 30;
    Button->Radius = 5;

    auto TextWidget = NewWidget<RTextWidget>(Button);
    TextWidget->Text = Text;
    TextWidget->FontSize = 12;
    TextWidget->Anchor.CenterIn(Button);
    TextWidget->VerticalAlignment = ETextVerticalAlignment::Center;
    TextWidget->HorizontalAlignment = ETextHorizontalAlignment::Center;

    auto Mouse = NewWidget<RMouseWidget>(Button);
    Mouse->Anchor.Fill(Button);
    Mouse->OnClicked.BindLambda([InOnClick](const SMouseButtonEvent&)
    {
        InOnClick();
        return true;
    });
    RMouseWidgetWeakPtr MouseWeakPtr = Mouse;

    Button->FillColor=[MouseWeakPtr](){
        static const SColor NormalColor = SColor::FromHexRGB(0x0078D4);
        static const SColor HoverColor = SColor::FromHexRGB(0x006EC1);
        return MouseWeakPtr->IsHovered()?
        HoverColor:
        NormalColor;
    };

    return Button;
}